package com.example.extenddemo.extendsDemo;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @ClassName GetBeanUtils
 * @Description TODO
 * @Author 郭敖
 * @Date 2024年01月04日 22:05
 * @Version 1.0
 */
@Component
@Slf4j
public class GetBeanUtils<T> implements ApplicationContextAware, InitializingBean,BeanPostProcessor {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public T getBean(String beanName){
        return (T) context.getBean(beanName);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("Bean对象初始化时执行的方法");
    }

    @PostConstruct
    public void beforeInit(){
        log.info("Bean对象初始化之前的操作");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        //log.info("Bean对象的名称为：{}",beanName);
        return bean;
    }
}
